<script setup>
import Info from './info.vue'
import { useConfirm } from '@/hooks/useConfirm'
// import mock from '~mock/menu'
import {onMounted, ref} from 'vue'
import {pageApi,deleteApi} from "../../../api/menu";
import {ElMessage, ElMessageBox} from "element-plus";

const tableData = ref({})
// const tableData = ref(mock || [])

// 表单状态
const filterState = ref(true)
// 表单数据
const formData = ref({
  page: 1,
  size: 10,
  name: '',
  type: 0
})

onMounted(()=>{
  loadData()
})

const loadData=()=>{
  pageApi(formData.value).then(r=>{
    tableData.value=r.data;
  })
}
// 编辑
const authEditRef = ref()
const onAuthEdit = (row) => {
  authEditRef.value.state = true
  // 回显
  if (row) {
    authEditRef.value.formData.id = row.id
    authEditRef.value.formData.component = row.component
    authEditRef.value.formData.icon = row.icon // 1-菜单， 2-按钮
    authEditRef.value.formData.level = row.level
    authEditRef.value.formData.name = row.name
    authEditRef.value.formData.parentId = row.parentId //1-缓存,0-不缓存
    authEditRef.value.formData.path = row.path
    authEditRef.value.formData.sort = row.sort
    authEditRef.value.formData.type = row.type
  }
}

// 权限删除
const onDelete = (row) => {
  ElMessageBox.confirm('确定删除该条数据吗?', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    //接口请求 删除角色
    const  r=await deleteApi({id:row.id});
    if(r.code==0){
      ElMessage({
        type: 'success',
        message: '删除成功'})
        //重新加载请求
        loadData()
      }else {
        ElMessage.error(r.data)
      }
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '用户取消'
    })
  })
}

</script>
<template>
  <Box>
    <!-- 逻辑层 -->
    <Info ref="authEditRef" @flush="loadData"/>
    <!-- 逻辑层 -->

    <!-- 筛选 -->
    <template #filter v-if="filterState">
      <el-form :inline="true" :model="formData">
        <el-form-item label="菜单名称">
          <el-input placeholder="请输入菜单名称" v-model="formData.name" />
        </el-form-item>
        <el-form-item label="类型">
          <el-select placeholder="菜单类型" v-model="formData.type"  style="width: 120px">
            <el-option label="菜单" value="1"/>
            <el-option label="权限" value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"> <span class="iconfont icon-sousuo"></span>搜索 </el-button>
          <el-button type="default"> 重置 </el-button>
        </el-form-item>
      </el-form>
    </template>
    <!-- 筛选 -->

    <!-- 按钮组 -->
    <el-row>
      <el-col :span="12">
        <el-button type="primary" @click="onAuthEdit">
          <span class="iconfont icon-jiahao"></span>添加
        </el-button>
      </el-col>

      <el-col :span="12" style="text-align: right">
        <el-button circle @click="filterState = !filterState">
          <span class="iconfont icon-sousuo"></span>
        </el-button>
        <el-button circle>
          <span class="iconfont icon-shuaxin"></span>
        </el-button>
      </el-col>
    </el-row>
    <!-- 按钮组 -->

    <el-table :data="tableData.data" :border="true" class="qfTable" row-key="id" default-expand-all>
      <el-table-column
        label="菜单名称"
        prop="name"
        align="left"
        width="140"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="图标"
        prop="icon"
        align="center"
      ></el-table-column>
      <el-table-column
        label="排序"
        prop="sort"
        align="center"
      ></el-table-column>
      <el-table-column label="类型" align="center">
        <template #default="scoped">
          <el-tag v-if="scoped.row.type === 1">菜单</el-tag>
          <el-tag type="success" v-else>权限</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="路径|权限" prop="path" align="center"></el-table-column>
      <el-table-column label="组件" prop="component" align="center"></el-table-column>
      <el-table-column label="菜单等级" prop="level" align="center"></el-table-column>
      <el-table-column label="创建时间" prop="ctime" align="center"></el-table-column>
      <el-table-column label="操作" align="center" width="120" fixed="right">
        <template #default="scoped">
          <el-button type="primary" link size="small" @click="onAuthEdit(scoped.row)">
            <span class="iconfont icon-bianji"></span>修改
          </el-button>
          <el-button type="primary" link size="small" @click="onDelete(scoped.row)">
            <span class="iconfont icon-shanchu"></span>删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>


  </Box>
</template>

<style lang="scss" scoped></style>
